package com.levi.config;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.logging.Logger;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson客户端使用Bloom过滤器拦截无效请求，解决缓存穿透
 * 项目初始化的时候初始化布隆过滤器(例如把商品编号都放进去)，用户发过来的请求(带商品编号)先经过布隆过滤器过滤，过滤掉的返回null
 *
 */
@Configuration
@Slf4j
public class RedissinConfig {

	@Value("${redisson.address}")
	private String addressUrl;

	 @Bean
	 public RedissonClient redissonClient() {
		 log.info("addressUrl:{}",addressUrl);
	 Config config = new Config();
	 config.useSingleServer()
	 .setAddress(addressUrl)
	 .setRetryInterval(5000)
	 .setTimeout(10000)
	 .setConnectTimeout(10000);
	 return Redisson.create(config);
	 }

	@Bean
	public RBloomFilter<String> bloomFilter(RedissonClient redissonClient) {
		RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("bloom");
		bloomFilter.tryInit(1000000L, 0.01);
		return bloomFilter;
	}

}
